#!/bin/bash
message() {
    echo "$@" 1>&2
}
PROG=${0##*/}
usage() {
    cat <<EO_ECHO 1>&2
usage: $PROG -r REPO [ -o OUTPUT ]
  -r, --remote=REPO   specify a remote repository, you must insert
                      it in this script if it is not existent,
                      just use an identifier, rather than a full
                      URL
  -d, --local=NAME    specify a local directory, which is a SVN
                      repository, the same as the option '-r',
                      just use an identifier, and some changes
                      on this script is needed if you add new
                      repositories
  -u, --username=USER     specify username for authentication
  -p, --password=PASSWD   specify password for authentication

  -h, --help              print this screen

                     NOTE:
                          The username and password can also be
                          filled in the script, currently the
                          command line arguments have no effect.

Author: zerox (zxia@helixmicro.com.cn)
Send bug report/comments to zeroxia@gmail.com
EO_ECHO
    exit 0
}

## options parsing
parsed=$(getopt --shell bash --name ${0##*/} \
    --options r:l:o:u:p:h \
    --longoptions remote:,local:,output:,username:,password:,help \
    -- "$@")
if [[ $? -ne 0 ]]; then
    echo "error when parsing options, quit..." 1>&2
    exit 1
fi
eval set -- "$parsed"
while true; do
    case "$1" in
        -h|--help        ) usage ;;                      # exit immediately
        -r|--remote      ) remote=$2;        shift 2 ;;
        -l|--local       ) local=$2;         shift 2 ;;
        -o|--output      ) output=$2;        shift 2 ;;
        -u|--username    ) cmd_username=$2;  shift 2 ;;
        -p|--password    ) cmd_password=$2;  shift 2 ;;
        --               ) shift; break ;;               # end of command line options
        *) # invalid option
        echo "error: unknown option: \`$arg'" 1>&2
        echo "Try run the script with argument \`-h' or \`--help'" 1>&2
        exit 2
        ;;
    esac
done

### option defaults
remote=${remote:-csie}
local=${local:-csie}
output=${output:-${local}};
if [[ -n "$cmd_username" && -n "$cmd_password" ]]; then
    auth_str="--username=$cmd_username --password=$cmd_password"
fi

### option processing

case $remote in
    google|gg)
        remote_repo=http://zerox.googlecode.com/svn
    ;;
    csie)
        remote_repo=http://opensvn.csie.org/cpreport
    ;;
    *)
        message "error: $remote: unknown remote repository ID"
        exit -1
    ;;
esac

local_repo_path=/mnt/d/zerox/work/script/sync
case $local in
    google|csie)
        local_repo=$local_repo_path/$local
    ;;
    gg) # an alias for google
        local_repo=$local_repo_path/google
    ;;
    *)
        message "error: $local: unknown local repository ID"
        exit -1
    ;;
esac

### show the options
message "__The Options__"
message "Remote repo: $remote_repo"
message "Local  repo: $local_repo"
message "Output name: $output"
message "Auth.  str.: $auth_str"

begin_date=$(date)
begin_time=$(date +%s)
echo "? operation begin: ${begin_date}" 1>&2

# test remote repository
#remote_repo=file:///d:/zerox/work/script/sync/google

local_rev=$(svnlook youngest $local_repo)
if [[ $? -ne 0 ]]; then
    message "error occured when accessing local repository"
    exit -1
fi
message "Local  revision: $local_rev"

message "Getting remote revision number, please be patient..."
remote_rev=$(svn info $remote_repo | grep -Po '(?<=^Revision: )\d+')
if [[ $? -ne 0 ]]; then
    message "error occured when accessing remote repository"
    exit -1
fi
message "Remote revision: $remote_rev"

if [[ $local_rev -eq $remote_rev ]]; then
    message "The two targets are of the same revision"
    exit 0
elif [[ $local_rev -lt $remote_rev ]]; then
    message "The local revision is older than the remote!!!"
    message "What's wrong???"
else
    message "Okay, the local revision is newer than the remote"
    from_rev=$((remote_rev + 1))
    to_rev=$local_rev
    dump_file=dump_${output}_r${from_rev}-${to_rev}
    message "Dumping revisions r$from_rev:$to_rev to file '$dump_file'..."
    svnadmin dump --incremental -r$from_rev:$to_rev $local_repo | gzip -9 > $dump_file.gz
    if [[ -f $dump_file.gz ]]; then
        message "Dumping okay, now creating MD5 checksum..."
        md5sum $dump_file.gz | tee $dump_file.md5
    else
        message "Ooops, no dump file found, please double-check it"
    fi
fi

end_date=$(date)
end_time=$(date +%s)
echo "? operation begin: ${begin_date}" 1>&2
echo "? operation   end: ${end_date}" 1>&2
echo "? time   consumed: $((end_time - begin_time)) second(s)" 1>&2

# SVN information
# $LastChangedRevision: 102 $
# $LastChangedBy: zeroxia $
# $LastChangedDate: 2007-06-15 21:14:01 +0800 (Fri, 15 Jun 2007) $
# $HeadURL: https://opensvn.csie.org/zerox/cpreport/trunk/repo-dump.sh $
